package com.yq.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.backend.dto.CardUserDto;
import com.yq.backend.mapper.CardUserMapper;
import com.yq.backend.mapper.GradesMapper;
import com.yq.backend.pojo.CardUser;
import com.yq.backend.pojo.Result;
import com.yq.backend.pojo.SysDict;
import com.yq.backend.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @descriptions: 会员管理
 * @author: yunqiang
 * @date: 2022/5/3 15:03
 */
@Slf4j
@Service
public class MemberServiceImpl implements MemberService {


    @Autowired
    private GradesMapper gradesMapper;

    @Autowired
    private CardUserMapper cardUserMapper;

    /**
     * 等级列表
     *
     * @param name
     * @param pageSize
     * @param pageNow
     * @return
     */
    @Override
    public Result getGrades(String name, int pageSize, int pageNow) {
        if (pageNow > 0) {
            pageNow = (pageNow - 1) * pageSize;
        } else {
            pageNow = 0;
        }
        long total = gradesMapper.getGradeCount(name);
        List<SysDict> grades = gradesMapper.getGrades(name, pageNow, pageSize);
        if (CollectionUtils.isEmpty(grades)) {
            return Result.noResult();
        }
        return Result.success(total, grades);
    }

    /**
     * 新增会员
     *
     * @param cardUser
     * @return
     */
    @Override
    public Result addMembers(CardUser cardUser) {
        Integer count = cardUserMapper.selectCount(new QueryWrapper<CardUser>().lambda().eq(CardUser::getUname, cardUser.getUname()));
        if (count > 0) {
            return Result.error(502, "用户名已存在!");
        }
        cardUser.setId(String.valueOf(UUID.randomUUID()));
        cardUser.setCreatetime(new Date());
        cardUser.setUpdatetime(new Date());
        cardUser.setPasswd("123456");
        cardUser.setPic("123");
        cardUser.setIdcard("123");
        cardUserMapper.insert(cardUser);
        return Result.success();
    }

    /**
     * 修改会员
     *
     * @param cardUser
     * @return
     */
    @Override
    public Result upMembers(CardUser cardUser) {
        cardUserMapper.updateById(cardUser);
        return Result.success();
    }

    /**
     * 删除会员
     *
     * @param id
     * @return
     */
    @Override
    public Result delMembers(String id) {
        cardUserMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询会员
     *
     * @param id
     * @return
     */
    @Override
    public Result getmembersId(Integer id) {
        CardUser cardUser = cardUserMapper.selectById(id);
        return cardUser == null ? Result.noResult() : Result.success(cardUser);
    }

    /**
     * 会员列表
     *
     * @param cardUserDto
     * @return
     */
    @Override
    public Result getMembers(CardUserDto cardUserDto) {
        int pageNow = cardUserDto.getPageNow();
        int pageSize = cardUserDto.getPageSize();
        if (pageNow > 0) {
            pageNow = (pageNow - 1) * pageSize;
        } else {
            pageNow = 0;
        }
        long total = cardUserMapper.getMembersCount(cardUserDto);
        List<CardUser> cardUsers = cardUserMapper.getMembers(cardUserDto, pageNow, pageSize);
        if (CollectionUtils.isEmpty(cardUsers)) {
            return Result.noResult();
        }
        return Result.success(total, cardUsers);
    }

    @Override
    public Result allGrade() {
        List<SysDict> grades = gradesMapper.selectList(new QueryWrapper<SysDict>().lambda().eq(SysDict::getDictType,"grade"));
        return Result.success(grades);
    }
}
